﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using Facebook;

namespace FeedBack {
    public class Story {
        //"message": "CCTVB!!!",
        //"story": "PassionTimes 熱血時報 shared 自由市場's photo.",
        //"picture": "https://fbcdn-sphotos-c-a.akamaihd.net/hphotos-ak-xpa1/v/t1.0-9/s130x130/10393995_267713170019034_1415758717313800447_n.jpg?oh=930373322eb7f662e753ddb188e41376&oe=54C7E050&__gda__=1421176502_33d059b194f0faf3404a7f4f0961646d",
        //"link": "https://www.facebook.com/117904764999876/photos/a.118177608305925.2126.117904764999876/267713170019034/?type=1",
        //"source": "https://fbcdn-video-e-a.akamaihd.net/hvideo-ak-xfp1/v/t42.1790-2/10290074_10154675843900788_363278391_n.mp4?oh=4fef9727aac3bbf3f112e78c3e2ec79f&oe=54337476&__gda__=1412666349_03edc4cc0b994711d74c454162086df9",
        //"caption": "pic.twitter.com",
        //"description": "Get the whole picture - and other photos from Richard Lo", 
        public string Message { get; set; }
        public string StoryName { get; set; }
        public string Picture { get; set; }
        public Uri Link { get; set; }
        public Uri Source { get; set; }
        public string Caption { get; set; }
        public string Description { get; set; }
        public string Type { get; set; }
        public JsonObject Json { get; set; }

        public Story(JsonObject json) {
            Json = json;
            if (json.Keys.Contains("type")) {
                Type = json["type"].ToString();
            } 
            if (json.Keys.Contains("message")) {
                Message = json["message"].ToString();
            }
            if (json.Keys.Contains("story")) {
                StoryName = json["story"].ToString();
            }
            if (json.Keys.Contains("picture")) {
                Picture = json["picture"].ToString();
            }
            if (json.Keys.Contains("link")) {
                Link = new Uri(json["link"].ToString());
            }
            if (json.Keys.Contains("source")) {
                Source = new Uri(json["source"].ToString());
            }
            if (json.Keys.Contains("caption")) {
                Caption = json["caption"].ToString();
            }
            if (json.Keys.Contains("description")) {
                Description = json["description"].ToString();
            }

        }

        private static string CreateFileName(string candidate) {
            if (string.IsNullOrWhiteSpace(candidate)) {
                return candidate;
            }
            candidate = candidate.Replace('\n', ' ');
            candidate = candidate.Replace("  ", " ");
            if (candidate.Length > 100) {
                candidate = candidate.Substring(0, 100).Trim() + "...";
            }
            return candidate;
        }

        public string CandidateFileName(Uri link) {

            string filename = link.PathAndQuery;
            int start = filename.LastIndexOf("/") + 1;
            int end = filename.IndexOf("?");
            string suffix = "";
            if (end < start) {
                end = filename.Length;
            }
            else {
                suffix = string.Format("_{0:X}", link.Query.GetHashCode());
            }
            filename = filename.Substring(start, end - start);
            if (filename.Count(o => o == '%') > 5) {
                filename = WebUtility.UrlDecode(filename);
            }

            int extPos = filename.LastIndexOf(".");
            string ext = "";
            if (extPos >= 0) {
                ext = filename.Substring(extPos);
            }

            var filename2 = CreateFileName(this.Message);
            if (string.IsNullOrWhiteSpace(filename2)) {
                filename2 = CreateFileName(this.Description);

            }
            if (!string.IsNullOrWhiteSpace(filename2)) {
                filename = filename2 + suffix + ext;
            }
            else {
                filename = Path.GetFileNameWithoutExtension(filename) + suffix + ext;
            }
            return filename.RemoveIllegalPathCharacters(); ;

        }
    }
}
